<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="用户名" field="username">
            <a-input v-model="searchForm.username" placeholder="请输入用户名" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户类型:" field="user_type">
            <a-input v-model="searchForm.user_type" placeholder="请输入用户类型:" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户昵称" field="nickname">
            <a-input v-model="searchForm.nickname" placeholder="请输入用户昵称" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/users'

// 引用定义
const crudRef = ref()
const editRef = ref()

// 搜索表单
const searchForm = ref({
  username: '',
  user_type: '',
  nickname: '',
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/users/UsersInfo/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/users/UsersInfo/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/users/UsersInfo/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/users/UsersInfo/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/users/UsersInfo/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '密码', dataIndex: 'password', width: 180 },
  { title: '用户类型:', dataIndex: 'user_type', width: 180 },
  { title: '用户昵称', dataIndex: 'nickname', width: 180 },
  { title: '手机', dataIndex: 'phone', width: 180 },
  { title: '用户邮箱', dataIndex: 'email', width: 180 },
  { title: '用户头像', dataIndex: 'avatar', width: 180 },
  { title: '个人签名', dataIndex: 'signed', width: 180 },
  { title: '后台首页类型', dataIndex: 'dashboard', width: 180 },
  { title: '状态', dataIndex: 'status', width: 180 },
  { title: '最后登陆IP', dataIndex: 'login_ip', width: 180 },
  { title: '最后登陆时间', dataIndex: 'login_time', width: 180 },
  { title: '后台设置数据', dataIndex: 'backend_setting', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
